package o2o.thridpost.dada.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.jinyou.utils.common.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import o2o.config.PropConfigure;
import o2o.config.thirdpost.dada.DadaRequestClient;
import o2o.thridpost.dada.client.DadaApiResponse;
import o2o.thridpost.dada.client.DadaRequestParam;
import o2o.thridpost.dada.config.UrlConstant;
import o2o.thridpost.dada.model.to.AddDadaShopTO;
import o2o.thridpost.dada.model.to.UpdateDadaOrderTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author sunyufan deporation@qq.com
 * @date 2021/5/6 17:59
 * @description: DadaSopController
 */
@Slf4j
@RestController
@RequestMapping("/dada/shop")
public class DadaShopController {


    @Autowired
    private DadaRequestClient requestClient;

    /**
     * 查询商店详情信息
     */
    @RequestMapping(value = "/queryShop", method = RequestMethod.GET)
    public String queryShop(@RequestParam(value = "origin_shop_id") String originShopId) {
        Map<String, String> param = new HashMap<>(1);
        param.put("origin_shop_id", originShopId);

        DadaRequestParam<Map<String, String>> requestParam = new DadaRequestParam<>(param, requestClient.getAppConfig());

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.SHOP_DETAIL);

        log.info(apiResponse.toString());

        if (apiResponse.getCode() == 0) {
            return AjaxResult.successResult(apiResponse.getResult());
        }

        return "";
    }

    /**
     * 添加门店
     */
    @RequestMapping(value = "/addShop", method = RequestMethod.POST)
    public String addShop(@RequestBody List<AddDadaShopTO> addDadaShopTOs) {

        log.info("参数接受为" + addDadaShopTOs.toString());

        DadaRequestParam<List<AddDadaShopTO>> requestParam = new DadaRequestParam<>(addDadaShopTOs, requestClient.getAppConfig());

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.SHOP_ADD_URL);

        log.info(apiResponse.toString());

        if (apiResponse.getCode() == 0) {
            return AjaxResult.successResult(apiResponse.getResult());
        } else if ("fail".equals(apiResponse.getStatus())) {
            return AjaxResult.errorResult(JSON.toJSONString(apiResponse.getResult()));
        } else {
            return AjaxResult.errorResult(apiResponse.getMsg());
        }
    }

    /**
     * 更新门店
     */
    @RequestMapping(value = "updateShop", method = RequestMethod.POST)
    public String updateShop(@RequestBody String updateShop) {
        log.info("参数接受为" + updateShop);

        JSONObject jsonObject = JSONObject.parseObject(updateShop);
        Map<String, Object> updateShopTO = (Map<String, Object>) JSONObject.parseObject(jsonObject.getString("updateShop"));
        if (!updateShopTO.containsKey("origin_shop_id")) {
            return AjaxResult.errorResult("请传入旧门店编码,origin_shop_id");
        } else {
            DadaRequestParam<Map<String, Object>> requestParam = new DadaRequestParam<>(updateShopTO, requestClient.getAppConfig());

            DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.UPDATE_SHOP);

            log.info(apiResponse.toString());

            if (apiResponse.getCode() == 0) {
                return AjaxResult.successResult("成功");
            } else {
                return AjaxResult.errorResult(apiResponse.getMsg());
            }

        }
    }

}
